import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
//权限
const AUDIT_KEY = 'AUDIT_KEY';
//菜单
const MENU_KEY = 'MENU_KEY';

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		auditlist:'[]',
		userInfo: {user:''},
		// Vue.prototype.serverURL = 'http://183.66.176.38:8083/GGsrm';
		// serverURL:'http://127.0.0.1:8088/srm',
		serverURL:'http://192.168.1.116:8088/srm',
		// serverURL:'http://172.16.0.40:8083/srm',
		// serverURL:'http://172.16.3.38:8083/srm',
		// serverURL:'https://srm.maxpeedingrods.cn/srm',//服务器地址c
		menuList:[],
		corpCode:'',
	},
	mutations: {
		login(state, provider) {
			state.corpCode=provider.corpCodeName;
			state.hasLogin = true;
			state.userInfo = provider;
			uni.setStorage({//缓存用户登陆状态
			    key: 'userInfo',  
			    data: provider  
			});
		},
		logout(state) {
			// let uri="http://192.168.2.85:8083/waltzge/a"
			let uri=store.state.serverURL;
			console.log(uri);
			uni.request({
				url: uri+'/a/logout?__ajax=json&__sid='+state.userInfo.sessionid,
				data: {},
				success: function(res) {
					if (res.data.result == "true") {
						this.timer = setTimeout(()=>{   //设置延迟执行
						     console.log(res.data);
						},1000)
					} else {
						this.$api.msg(res.data.message);
						this.logining = false;
						return;
					}
				}.bind(this),
				fail: function() {
					this.$api.msg('服务已停止,请稍后再试');
					this.logining = false;
					return;
				}.bind(this)
			});
			state.hasLogin = false;
			state.userInfo = {};
			state.auditlist='[]';
			uni.removeStorage({  
                key: 'userInfo'  
            })
		},
		// 1、获取用户权限信息：
		//   http://127.0.0.1:8980/js/a/authInfo?__sid=5fe9c7c45ded4425b03eff8f78179637
		// 2、获取用户菜单信息：
		//   http://127.0.0.1:8980/js/a/menuTree?__sid=5fe9c7c45ded4425b03eff8f78179637
		// 3、重新获取登录信息：
		//   http://127.0.0.1:8980/js/a/index.json?__sid=5fe9c7c45ded4425b03eff8f78179637
		// 4、获取当前用户信息：
		//   http://127.0.0.1:8980/js/a/sys/user/info.json?__sid=5fe9c7c45ded4425b03eff8f78179637
		setAuditList(state){
			let uri=store.state.serverURL+'/a/authInfo';
			//调用权限接口
			uni.request({
				 url: uri,
				 data: {
					__sid:state.userInfo.sessionid
				 },
				success: function(res) {
					state.auditlist=res.data.stringPermissions;
				}.bind(this),
				fail:function(){
					state.auditlist='[]';
				}.bind(this)
			});
		},
		setMenuList(state){
			let uri=store.state.serverURL+'/a/menuTree';
			//调用权限接口
			uni.request({
				 url: uri,
				 data: {
					 parentCode:'1294220506841382912',
					__sid:state.userInfo.sessionid
				 },
				success: function(res) {
					state.menuList=res.data;
				}.bind(this),
				fail:function(){
					state.menuList=[];
				}.bind(this)
			});
		}
	},
	actions: {
	
	}
})

export default store
